package uia.alumni.form;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
//import org.apache.commons.validator.DateValidator;
//import org.apache.commons.validator.EmailValidator;
//import org.apache.commons.validator.UrlValidator;
//import org.apache.commons.validator.DateValidator;


/**
 * A class which handles regex expressions for each fieldType.
 * It's possible to use this class as a standalone regex tester as well,
 * but it's main purpose is to check common patterns for input forms.
 *
 * http://gskinner.com/RegExr/
 * http://www.regular-expressions.info/examples.html
 * http://www.txt2re.com/
 *
 * "I took a speed reading course and read War and Peace in 20 minutes. It's about Russia."
 *    — Woody Allen
 *
 * @author Simon Zimmermann
 */
public class FieldRegex {

    // maybe no need to make em static when they are private, but hey. why not?
    private static final String zip =   "^\\d{4}$";
    private static final String email = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\." +
                                        "[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:" +
                                        "[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+" +
                                        "[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";
    // Norwegian type of dates.
    private static final String date =  "^(?:(?:0?[1-9])|(?:[12]\\d)|(?:3(?:" +
                                        "(?:0)|(?:1(?![\\.-/](?:(?:0?[2469])|" +
                                        "(?:11))))))(?!(?:[\\.-/]\\d{1,2}[\\.-/])" +
                                        "(?:(?:[02468][048]00)|(?:(?:\\d{2})?(?:(?:" +
                                        "[2468][048])|(?:[13579][26])|(?:0[48]))))))" +
                                        "[\\.-/](?:(?:0?[1-9])|(?:1[0-2]))[\\.-/](?:" +
                                        "(?:\\d{2})|(?:1\\d{3})|(?:\\d1\\d{2})|" +
                                        "(?:\\d{2}1\\d)|(?:\\d{3}1))$";
    // dd/mm/yyyy
   private static final String date2 =  "(0[1-9]|[1-9]|[12][0-9]|3[01])[ \\-/.]" +
                                        "(0[1-9]|1[012]|[1-9])[ \\-/.](19|20)\\d{2}";
   // url
   private static final String url =    ".*?((?:[a-z][a-z\\.\\d\\-]+)\\.(?:[a-z]" +
                                        "[a-z\\-]+))(?![\\w\\.])";
   // Untested html
   private static final String html =   "<([A-Z][A-Z0-9]*)[^>]*>(.*?)</\\1>";

  // phonenumber with 8 numbers
  private static final String phone =  "^(\\d{8}|\\d{12})$";
   
  /*("^.*\\p{Alpha}.*$")
    && pw.matches("^.*\\p{Digit}.*$")*/

   private static final String password =  "^[\\p{Alnum}!@#$%^&]{4,20}$";
    /**
    * Default Constructor.
    */
    public FieldRegex(){}

    /**
     * Returns a boolean based on a type. Should find a looser coupling,
     * but this seemed to be the only way at the moment.
     * 
     * @param type
     * @param string
     * @return
     */
    public boolean isValid(FieldType type, String string){
        switch (type){
            case URL:
                return testString(url, string);
            case EMAIL:
                return testString(email, string);
            case TEXT:
                return true; // should add a regex here.
            case DATE:
                return testString(date2, string);
            case ZIP:
                return testString(zip, string);
            case HTML:
                return testString(html, string);
            case PHONENUMBER:
                return testString(phone, string);
            case PW:
                return testString(password, string); // maybe check pw lenght and characters.
            default:
                return false;
        }
    }
    
    /**
     * A generic testing method for checking regex vs a string.
     * Can be used as a standalone method.
     * 
     * @param regex
     * @param string
     * @return
     */
    public boolean testString(String regex, String string){
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(string);
        return matcher.find();
    }
}
